﻿using System;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using MichMan.Utilities.Requests;
using Microsoft.Phone.Shell;
using MichMan.Utilities;
using Microsoft.Phone.Controls;
using System.IO.IsolatedStorage;

namespace MichMan.Utilities.Requests
{
    public class PhoneCacheManager : CacheManager
    {
        private PhoneCacheManager()
        {
        }

        public static void Register()
        {
            CacheManager.Instance = new PhoneCacheManager();
        }

        public override ICache GetCache(string policy)
        {
            switch (policy)
            {
                case "PageState":
                    return new PageStateCache();
                case "ApplicationState":
                    return new ApplicationStateCache();
                case "IsolatedStorageSettings":
                    return new IsolatedStorageSettingsCache();
                case "IsolatedStorageFile":
                    return new IsolatedStorageFileCache();
                default:
                    return base.GetCache(policy);
            }
        }

        public override System.Collections.Generic.IEnumerable<string> Policies
        {
            get
            {
                return NewPolicies().Union(base.Policies);
            }
        }

        private System.Collections.Generic.IEnumerable<string> NewPolicies()
        {
            yield return "PageState";
            yield return "ApplicationState";
            yield return "IsolatedStorageSettings";
            yield return "IsolatedStorageFile";
        }
    }

    /// <summary>
    /// TODO:  This won't actually work.  It needs to keep it's own copy 
    /// of the page state and then do a to/from copy at the correct time.
    /// </summary>
    public class PageStateCache : CacheWrapper
    {
        protected override ICache GetWrapped()
        {
            PhoneApplicationPage page = Application.Current.RootVisual.GetFirstDescendant<PhoneApplicationPage>();
            if (page != null)
            {
                return new SimpleDictionaryCache(page.State);
            }
            return null;
        }
    }

    public class ApplicationStateCache : CacheWrapper
    {
        protected override ICache GetWrapped()
        {
            return new SimpleDictionaryCache(PhoneApplicationService.Current.State);
        }
    }

    public class IsolatedStorageSettingsCache : CacheWrapper
    {
        protected override ICache GetWrapped()
        {
            return new SimpleDictionaryCache(IsolatedStorageSettings.ApplicationSettings);
        }

        public override bool Insert(string key, object o, TimeSpan? lifetime)
        {
            try
            {
                return base.Insert(key, o, lifetime);
            }
            finally
            {
                IsolatedStorageSettings.ApplicationSettings.Save();
            }
        }

        public override void Delete(string key)
        {
            base.Delete(key);
            IsolatedStorageSettings.ApplicationSettings.Save();
        }
    }
}
